//
//  RORequestManager.m
//  Runone
//
//  Created by runone02 on 16/12/19.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "RORequestManager.h"
#import "AppLocalManager.h"
#import "Define.h"

@interface AFHTTPSessionManager (Shared)
// 设置单例
+ (instancetype)sharedManager;
@end

@implementation AFHTTPSessionManager (Shared)
+ (instancetype)sharedManager {
    static AFHTTPSessionManager *_instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [AFHTTPSessionManager manager];
        _instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/plain", @"text/json", @"text/javascript", @"text/html", nil];
    });
    return _instance;
}
@end
@interface  RORequestManager()



@end

@implementation RORequestManager

//+ (NSString *)strone {
//    
//    return [[AppLocalManager sharedManager] getDataByKey:ACCESS_TOKEN];
//}

+ (void)POST:(NSString *)urlString parameters:(id)parameters responseSerializerType:(ROResponseSerializerType)type success:(void(^)(id responseObject))success failure:(void(^)(NSError *error))failure {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *value = [NSString stringWithFormat:@"Bearer %@",[[AppLocalManager sharedManager] getDataByKey:ACCESS_TOKEN]];
    if (value) {
        [manager.requestSerializer setValue:value forHTTPHeaderField:@"Authorization"];
    }
    if (type != ROResponseSeializerTypeJSON && type != ROResponseSeializerTypeDefault) {
        manager.responseSerializer = [self responseSearalizerWithSerilizerType:type];
    }
    AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    policy.allowInvalidCertificates = YES;
    manager.securityPolicy          = policy;
    
    [manager POST:urlString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(responseObject);
        }
        NSLog(@"%@", responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (AFHTTPResponseSerializer *)responseSearalizerWithSerilizerType:(ROResponseSerializerType)serilizerType {
    
    switch (serilizerType) {
            
        case ROResponseSeializerTypeDefault
            : // default is JSON
            return [AFJSONResponseSerializer serializer];
            break;
            
        case ROResponseSeializerTypeJSON: // JSON
            return [AFJSONResponseSerializer serializer];
            break;
            
        case ROResponseSeializerTypeXML: // XML
            return [AFXMLParserResponseSerializer serializer];
            break;
            
        case ROResponseSeializerTypePlist: // Plist
            return [AFPropertyListResponseSerializer serializer];
            break;
            
        case ROResponseSeializerTypeCompound: // Compound
            return [AFCompoundResponseSerializer serializer];
            break;
            
        case ROResponseSeializerTypeImage: // Image
            return [AFImageResponseSerializer serializer];
            break;
            
        case ROResponseSeializerTypeData: // Data
            return [AFHTTPResponseSerializer serializer];
            break;
            
        default:  // 默认解析器为 JSON解析
            return [AFJSONResponseSerializer serializer];
            break;
    }
}
#pragma mark -上传
+ (void)POST:(NSString *)urlString parameters:(id)parameters imagesOrVideos:(NSArray <NSString *>*)imagesArr responseSerializerType:(ROResponseSerializerType)type constructingBodyWithBlock:(void(^)(id<AFMultipartFormData> fromData))block success:(void(^)(id responseObject))success failure:(void(^)(NSError *error))failure {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager sharedManager];
    // 请求头
        NSString *value = [NSString stringWithFormat:@"Bearer %@",[[AppLocalManager sharedManager] getDataByKey:ACCESS_TOKEN]];
        if (value) {
            [manager.requestSerializer setValue:value forHTTPHeaderField:@"Authorization"];
        }
    // 如果不是JSON 或者 不是Default 才设置解析器类型
    if (type != ROResponseSeializerTypeJSON && type != ROResponseSeializerTypeDefault) {
        manager.responseSerializer = [self responseSearalizerWithSerilizerType:type];
    }
    // https证书设置
    /*
     AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
     policy.allowInvalidCertificates = YES;
     manager.securityPolicy  = policy;
     */
    [manager POST:urlString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if (block) {
            block(formData);
        }

    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@", responseObject);
        if (success) {
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)cancelAllRequests {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager sharedManager];
    [manager.operationQueue cancelAllOperations];
}



@end
